<template>
  <div class="content center">
    <van-image
      round
      width="3rem"
      height="3rem"
      :src="require('@/assets/EvinChan.jpg')"
      style="border:.2rem solid rgba(0, 206, 201,0.2);"
    />
    <van-divider></van-divider>
    <div style="font-size:.5rem;color:rgba(0, 206, 201,0.5);font-weight:600">欢迎使用在线协同编辑图片系统</div>
    <van-divider></van-divider>
    <van-form @submit="login">
      <van-field
        v-model="loginForm.username"
        name="username"
        label="账号"
        placeholder="请输入平台账号"
        :rules="[{ required: true, message: '请输入登录名称', trigger: 'blur' }]"
      />
      <van-field
        v-model="loginForm.password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入登录密码', trigger: 'blur' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登陆</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        // username: '陈俊铭',
        // password: '123456',
        username: '',
        password: ''
      }
    }
  },
  methods: {
    showPopup() {
      this.show = true
    },
    onConfirm(value, index) {
      // console.log(`当前值：${value}, 当前索引：${index}`)
      this.loginForm.value = index + 1
      this.loginForm.role_name = value
      this.show = false
    },
    onCancel() {
      this.show = false
    },
    // 登陆提交
    async login() {
      // console.log(this.loginForm)
      if (this.loginForm.value === '') {
        this.$toast.fail('登陆编码/密码，无法登陆')
        return
      }
      // this.$refs.loginFormRef.validate(async valid => {
      // if (!valid) return
      const { data: res } = await this.$http.post('login', this.loginForm)
      if (res.meta.status !== 200) return this.$toast.fail(res.meta.msg)
      // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
      //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
      //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
      window.sessionStorage.setItem('token', res.data.token)
      // 将用户名存入seessionStorage
      window.sessionStorage.setItem('username', res.data.username)
      // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
      // if (this.loginForm.value === 1) {
      // this.loading = true
      const { data: resuser } = await this.$http.get('users', {
        params: { query: window.sessionStorage.getItem('username'),
          // 当前的页数
          pagenum: 1,
          // 当前每页显示多少条数据
          pagesize: 10 }
      })
      // console.log(resuser, '=======resuser')
      if (resuser.meta.status !== 200) {
        return this.$toast.fail('获取用户角色失败')
      }
      this.userData = resuser.data.users
      // console.log(this.userData, '======userdata')
      if (this.userData[0].role_name === '客户' || this.userData[0].role_name === '老板' || this.userData[0].role_name === '超级管理员') {
        this.$toast.success(res.meta.msg)
        this.loginForm = {
          username: '',
          password: ''
        }
        this.$router.push(`/mobile/library?user=${this.loginForm.username}`)
      } else {
        return this.$toast.fail('无权限登陆，停止操作！')
      }
      // }
      // })
    }
  }
}
</script>

<style lang="less" scoped>
.content{
  padding: 1.5rem 0;
  width: 100%;
  flex-direction: column;
  color: #999;
  /deep/.van-cell__value--alone{
    text-align: center;
  }
}
</style>
